//
//  PS_PasswordViewController.m
//  PolicySystem
//
//  Created by 丹丹 on 2018/10/24.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_PasswordViewController.h"

@interface PS_PasswordViewController ()
@property (strong, nonatomic) IBOutlet UIButton *getCodeButton;
@property (strong, nonatomic) IBOutlet UITextField *surePassWordTF;
@property (strong, nonatomic) IBOutlet UITextField *passwordTF;
@property (strong, nonatomic) IBOutlet UITextField *codeTF;
@property (strong, nonatomic) IBOutlet UITextField *phoneTF;
@property (strong, nonatomic) IBOutlet UIView *codeView;

@property (strong, nonatomic) IBOutlet UILabel *secondLabel;

@end

@implementation PS_PasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"忘记密码";
    
    _getCodeButton.layer.borderWidth = 0.5;
    _getCodeButton.layer.borderColor = RGBCOLOR(238, 114, 111).CGColor;//238 114 111
    _getCodeButton.layer.cornerRadius = 10;
    
    _codeView.layer.borderWidth = 0.5;
    _codeView.layer.borderColor = RGBCOLOR(238, 114, 111).CGColor;//238 114 111
    _codeView.layer.cornerRadius = 10;
    
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _codeTF.keyboardType = UIKeyboardTypeNumberPad;
    [_phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_codeTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_passwordTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_surePassWordTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)getNewPasswordAction:(id)sender {
    
    if (_phoneTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入手机号" toView:self.view];
        
        return;
    }
    
    if (_phoneTF.text.length < 11) {
        
        [MBProgressHUD showError:@"请输入正确手机号" toView:self.view];
        
        return;
    }
    
    if (_codeTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入验证码" toView:self.view];
        
        return;
    }
    if (_passwordTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入密码" toView:self.view];
        
        return;
    }
    
    if (_passwordTF.text.length < 6 || _passwordTF.text.length>18) {
        
        [MBProgressHUD showError:@"请输入正确密码" toView:self.view];
        
        return;
    }
    if (_surePassWordTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入确认密码" toView:self.view];
        
        return;
    }
    
    if (_surePassWordTF.text.length < 6 || _surePassWordTF.text.length > 18) {
        
        [MBProgressHUD showError:@"请输入正确确认密码" toView:self.view];
        
        return;
    }
    
    if (![_passwordTF.text isEqualToString:_surePassWordTF.text]) {
        
        [MBProgressHUD showError:@"两次密码不一样" toView:self.view];
        
        return;
    }

    [Utils post:CHANGPWD parameters:@{@"mobile":_phoneTF.text,@"code":_codeTF.text,@"password":_passwordTF.text}  success:^(NSInteger recode, id jsonObject) {
        
        [MBProgressHUD showSuccess:[jsonObject objectForKey:@"msg"] toView:self.view];

        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}
- (IBAction)getCode:(id)sender {
    
    if (_phoneTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入手机号" toView:self.view];
        
        return;
    }
    
    [Utils post:YANZHENGMA parameters:@{@"mobile":_phoneTF.text} success:^(NSInteger recode, id jsonObject) {
        
        [self startAnimation];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
- (void)textFieldDidChange:(UITextField *)textField

{
    
    if (textField == _phoneTF) {
        
        if (textField.text.length > 11) {
            
            textField.text = [textField.text substringToIndex:11];
            
        }
    }
    if (textField == _codeTF) {
        
        if (textField.text.length > 8) {
            
            textField.text = [textField.text substringToIndex:8];
            
        }
    }
    
    if (textField == _passwordTF) {
        
        if (textField.text.length > 18) {
            
            textField.text = [textField.text substringToIndex:18];
            
        }
    }
    if (textField == _surePassWordTF) {
        
        if (textField.text.length > 18) {
            
            textField.text = [textField.text substringToIndex:18];
            
        }
    }
    
    
    
}

- (void)startAnimation
{
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self->_codeView.hidden = YES;
                self->_getCodeButton.hidden = NO;
                [self->_getCodeButton setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
                self->_getCodeButton.layer.borderColor = RGBCOLOR(238, 114, 111).CGColor;
                [self->_getCodeButton setTitleColor:RGBCOLOR(238, 114, 111) forState:UIControlStateNormal];
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self->_getCodeButton.hidden = YES;
                self->_codeView.hidden = NO;
                self->_secondLabel.text = [NSString stringWithFormat:@"%d",seconds];
            });
            time--;
        }
    });
    dispatch_resume(_timer);
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
